package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.TravelPaper;
import cn.wolfcode.trip.base.domain.TravelPaperContent;
import cn.wolfcode.trip.base.mapper.TravelPaperContentMapper;
import cn.wolfcode.trip.base.mapper.TravelPaperMapper;
import cn.wolfcode.trip.base.mapper.TravelMapper;
import cn.wolfcode.trip.base.query.TravelPaperQuery;
import cn.wolfcode.trip.base.service.ITravelPaperService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class TravelPaperServiceImpl implements ITravelPaperService{
    @Autowired
    private TravelPaperMapper travelPaperMapper;

    @Autowired
    private TravelPaperContentMapper travelPaperContentMapper;

    @Override
    public PageInfo selectForList(TravelPaperQuery qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(), qo.getOrderBy());
        List<TravelPaper> list = travelPaperMapper.selectForList(qo);
        return new PageInfo(list);
    }

    @Override
    public void saveOrUpdate(TravelPaper travelPaper) {
        TravelPaperContent travelPaperContent = travelPaper.getContent();
        travelPaper.setReleaseTime(new Date());
        if(travelPaper.getId()==null){
            travelPaperMapper.insert(travelPaper);
            travelPaperContent.setId(travelPaper.getId());
            travelPaperContentMapper.insert(travelPaperContent);
        }else{
            travelPaperMapper.updateByPrimaryKey(travelPaper);
        }
    }

    @Override
    public List<TravelPaper> selectAll() {
        return travelPaperMapper.selectAll();
    }

    @Override
    public TravelPaper selectByTravelPaperId(Long travelPaperId) {
        return travelPaperMapper.selectByPrimaryKey(travelPaperId);
    }


}
